import api from "../../api/index.js";
import cacheApi from "../../utils/cache.js";
Component({
  options: {
    addGlobalClass: true,
  },
  /**
   * 组件的对外属性，是属性名到属性设置的映射表
   */
  properties: {
    money: Number, //传入组件的金额
    remark: String,
    extData: Object,
    show: Boolean,
    useCard: { //是否使用会员卡，默认为是
      type: Boolean,
      value: true
    },
    payOrderId:String,
  },

  /**
   * 组件的内部数据，和 properties 一同用于组件的模板渲染
   */
  data: {
    payType: 'wxPay', // 默认微信=wxPay，alipay支付宝
    alipayOpenMod: '0',
    memberCard: {}, //会员卡详情
    memberCardPayMoney: 0, //会员卡支付金额
    onlinePayMoney: 0, //需要在线支付的金额
    payOrderId: 0, //后端的支付单id
  },
  // 组件数据字段监听器，用于监听 properties 和 data 的变化
  observers: {
    'show': function (show) {
      this.setData({
        alipayQrcode: null,
        alipayOpenMod: wx.getStorageSync('alipay')
      })
    },
    'money': function (newVal) {
      this.setData({
        onlinePayMoney: newVal
      });
    },
  },
  lifetimes: {
    attached() {
      getApp().initLanguage(this)
      let memberCard = wx.getStorageSync('memberCard')
      if (memberCard?.id) {
        this.setData({
          memberCard
        })
      }
    },
    detached() {
      // 在组件实例被从页面节点树移除时执行
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    close() {
      this.triggerEvent('cancel')
    },
    //单选框change
    payTypeChange(event) {
      this.setData({
        payType: event.detail,
        alipayQrcode: null,
      });
    },
    payTypeClick(event) {
      const {
        name
      } = event.currentTarget.dataset;
      this.setData({
        payType: name,
        alipayQrcode: null,
      });
    },
    //点击余额支付
    onChangeBalance(event) {
      this.setData({
        checkedBalance: event.detail,
      });
      let memberCard = this.data.memberCard;
      let memberCardPayMoney = this.data.memberCardPayMoney;
      if (event.detail) { //选中
        if (memberCard.id && memberCard.balance > 0) {
          let onlinePayMoney = this.data.money
          if (memberCard.balance >= this.data.money) {
            onlinePayMoney = 0
            memberCardPayMoney = this.data.money
          } else {
            onlinePayMoney = this.data.money - memberCard.balance
            memberCardPayMoney = memberCard.balance
          }
          this.setData({
            onlinePayMoney,
            memberCardPayMoney
          })
        }
      } else { //取消选中
        this.setData({
          onlinePayMoney: this.data.money,
          memberCardPayMoney: 0
        })
      }
    },
    async submit() {
      /**
       * 构造支付参数
       */
      let requestParam = {
        id: this.data.payOrderId,
        payOrderRecords: []
      }
      if (this.data.memberCardPayMoney > 0) {
        requestParam.payOrderRecords.push({
          payOrderId: this.data.payOrderId,
          payMethodType: "balancePay",
          payAmount: this.data.memberCardPayMoney,
          memberCardId: this.data.memberCard.id,
        })
      }
      if (this.data.onlinePayMoney > 0) {
        requestParam.payOrderRecords.push({
          payOrderId: this.data.payOrderId,
          payMethodType: this.data.payType,
          payAmount: this.data.onlinePayMoney,
          memberCardId: this.data.memberCard.id,
        })
      }
      api.paySubmit({
        data: requestParam
      }).then((res) => {
        if (this.data.memberCardPayMoney > 0) {
          //todo更新会员信息
          cacheApi.refreshMemberCard()
        }
        this.triggerEvent('ok', this.data)
      })
      // if (this.data.payType == 'wx') {
      //   wx.requestPayment({
      //     timeStamp: res.data.timeStamp,
      //     nonceStr: res.data.nonceStr,
      //     package: res.data.package,
      //     signType: res.data.signType,
      //     paySign: res.data.paySign,
      //     fail: aaa => {
      //       console.error(aaa)
      //       wx.showToast({
      //         title: '' + aaa,
      //         icon: 'none'
      //       })
      //     },
      //     success: () => {
      //       wx.showToast({
      //         title: this.data.$t.asset.success
      //       })
      //       this.triggerEvent('ok', this.data)
      //     }
      //   })
      // }
      if (this.data.payType == 'alipay') {
        this.setData({
          alipayQrcode: "/images/order/1.png"
        })
      }
    },
  }
})